<include file="../Public/_top" />
<script src="__THEME__/js/jquery.js?v={$site.sys_version}"></script>
<style>
  #uploadImg {
    position: absolute;
    top: 0;
    left: 0;
    bottom: 0;
    right: 0;
    width: 100px;
    height: 60px;
    opacity: 0;
    cursor: pointer;
  }
</style>
<div class="content">
  <div class="wrap">
    <include file="./_left" />
    <div class="con_main">
      <div class="con_tit">
        <ul class="con_link">
          <li class="on"><a href="javascript:;">学生列表 </a></li>
          <li><a href="{:U('public/Index/addStudent')}">新建学生</a></li>
          <li><a href="{:U('public/Index/recoveryStudent')}">恢复学生数据</a></li>
        </ul>
        <div class="search_wrap">
          <input class="search_inp" type="text" value="" id="stu_name" placeholder="按姓名搜索">
          <input class="search_btn" type="submit" value="" onclick="select_filter();">
        </div>
      </div>
      <div style="width:100%;height:70px;padding:15px;">
        <div class="sel_box">
          选择年级：
          <select id="grade" class="sel" value="" onchange="selectGrade();">
            <option value="">--------请选择--------</option>
            <option value="1">大班</option>
            <option value="2">中班</option>
            <option value="3">小班</option>
            <option value="4">混龄班</option>
          </select>
        </div>
        <div class="sel_box">
          选择班级：
          <select id="Class" class="sel" value="">
            <option value="0">--------请选择--------</option>
          </select>
        </div>
        <div class="sel_box">
          <div class="action_link tc" style="margin-top:15px;">
            <a class="dowm" href="javascript:select_filter();">确定</a>
          </div>
        </div>

        <div class="sel_box" style="position:relative;width:100px;">
          <div class="action_link tc" style="margin-top:15px;">
            <a class="dowm" href="javascript:void(0);">导入</a>
            <input type="file" id="uploadImg" name="upload_img" accept="">
          </div>
        </div>

      </div>
      <div class="con_tit">
        <table class="table table_title t7_3">
          <tr>
            <td>姓名</td>
            <td>性别</td>
            <td>出生日期</td>
            <td>顺产/剖腹</td>
            <td>出生体重（kg）</td>
            <td>操作</td>
          </tr>
        </table>
      </div>
      <div class="con_box" id="student_list"></div>
    </div>
  </div>
</div>
<div class="pop_wrap" id="pop_wrap" style="display:none;">
  <div class="pop_box">
    <div class="pop_head"><a class="close" href="javascript:close();">X</a>学生档案调档管理</div>
    <div class="pop_body">
      <dl class="diapei_dl">
        <dt>调至：</dt>
        <dd>
          <select id="class_id">
            <option value="0">请选择班级</option>
            <volist name="data" id="class">
              <option value="{$class.class_id}">{$class.class_name}</option>
            </volist>
          </select>
        </dd>
      </dl>
    </div>
    <ul class="pop_foot">
      <li><a href="javascript:close();">取消</a></li>
      <li><a href="javascript:doUpdateStuClass();">确定</a></li>
    </ul>
  </div>
</div>
<input type="hidden" id="stu_id">
<script>
  var class_id = "{$class_id}";
  if (class_id > 0) {
    $("#grade").attr("disabled", "disabled");
    $("#Class").attr("disabled", "disabled");
    $(document).ready(function () {
      $.ajax({
        type: "GET",
        url: "{:U('public/Index/getStudentList')}",
        data: "p=1&class_id=" + class_id,
        dataType: "json",
        success: function (data) {
          appendHtml(data);
        }
      });
    });
  }


  $('#uploadImg').on("change", function () {
    var file = this.files[0]
    if (!file) {
      return false
    }
    if (confirm('确认导入？')) {
      var formdata = new FormData();
      formdata.append('file', file);
      $.ajax({
        url: "{:U('public/TestData/import')}",
        type: 'POST',
        data: formdata,
        processData: false, //formdata已将数据序列化，无需在处理
        contentType: false, //formdata无需设置请求头
        dataType: 'json',
        success: function (res) {
          if (res.status == 1) {
            alert('上传成功')
          } else {
            alert(res.info || '上传失败')
          }

          // 清除file
          $('#uploadImg').val(null)
        }
      })
    }
  })

  init()

  function init() {
    var StuName = "{$params['stuName']}";
    var Grade = "{$params['grade']}";
    var Class = "{$params['class']}";

    $("#stu_name").val(StuName);
    $("#grade").val(Grade);
    $("#grade option").each(function () {
      if ($(this).val() == Grade) {
        $(this).attr('selected', true)
      }
    })
    if (Class || Grade || StuName) {
      $("#Class").val(Class);
      if (Grade != "") {
        $.ajax({
          type: "GET",
          url: "{:U('public/TestData/getClass')}",
          data: "class_type=" + Grade + "&class_mod=1",
          dataType: "json",
          success: function (data) {
            $("#Class").html("");
            $("#Class").append("<option value=''>----------------请选择----------------</option>");
            $.each(data, function (i, item) {
              if (item.class_id == Class) {
                $("#Class").append("<option value='" + item.class_id + "' selected>" + item.class_name + "</option>");
              } else {
                $("#Class").append("<option value='" + item.class_id + "'>" + item.class_name + "</option>");
              }
            });

            select_filter()
          }
        });
      } else {
        select_filter()
      }
    }
  }

  //筛选条件
  function select_filter() {
    var stu_name = $("#stu_name").val();
    var class_id = $("#Class").val();
    p = 1;
    $.ajax({
      type: "GET",
      url: "{:U('public/Index/getStudentList')}",
      data: "p=" + p + "&stu_name=" + stu_name + "&class_id=" + class_id,
      dataType: "json",
      success: function (data) {
        appendHtml(data);
      }
    });
  }
  function updateStuClass(id) {
    $("#stu_id").val(id);
    $("#pop_wrap").css("display", "block");
  }
  function doUpdateStuClass() {
    var class_id = $("#class_id").val();
    var stu_id = $("#stu_id").val();;
    if (class_id == 0) {
      alert("请选择班级");
      return;
    }
    $.ajax({
      type: "POST",
      url: "{:U('public/Index/UpdateStuClass')}",
      data: "class_id=" + class_id + "&stu_id=" + stu_id,
      dataType: "text",
      success: function (data) {
        if (isNaN(data)) {
          alert(data);
        } else {
          alert("调档成功");
          $("#pop_wrap").css("display", "none");
          select_filter();
        }
      }
    });
  }
  function selectGrade() {
    var grade = $("#grade").val();
    if (grade != "") {
      $.ajax({
        type: "GET",
        url: "{:U('public/TestData/getClass')}",
        data: "class_type=" + grade + "&class_mod=1",
        dataType: "json",
        success: function (data) {
          $("#Class").html("");
          $("#Class").append("<option value=''>----------------请选择----------------</option>");
          $.each(data, function (i, item) {
            $("#Class").append("<option value='" + item.class_id + "'>" + item.class_name + "</option>");
          });
        }
      });
    }
  }
  //追加html
  function appendHtml(data) {
    $("#student_list").html("");
    $("#student_list").append(data.data);
    var html = "<div class='pagebox'>";
    if (data.nowPage > 1) {
      page = data.nowPage - 1;
      html += "<a href=\"javascript:getList(" + page + ")\">上一页</a>";
    }
    for (var i = 1; i <= data.totalPages; i++) {
      if (i == data.nowPage) {
        html += "<a href=\"javascript:getList(" + i + ")\" class='on'>" + i + "</a>";
      } else {
        html += "<a href=\"javascript:getList(" + i + ")\" >" + i + "</a>";
      }
    };
    if (data.totalPages > 1 && data.nowPage < data.totalPages) {
      page = data.nowPage + 1;
      html += "<a href=\"javascript:getList(" + page + ")\" id=\"getList\">下一页</a>";
    }
    html += "</div>";
    $("#student_list").append(html);
  }

  //查看更多
  function getList(page) {
    p = page;
    ajaxBang();
  }
  //请求事件
  function ajaxBang() {
    var stu_name = $("#stu_name").val();
    var Class = $("#Class").val();
    if (Class > 0) {
      class_id = Class;
    }
    $.ajax({
      type: "GET",
      url: "{:U('public/Index/getStudentList')}",
      data: "p=" + p + "&class_id=" + class_id + "&stu_name=" + stu_name,
      dataType: "json",
      success: function (data) {
        appendHtml(data);
      }
    });
  }
  function EditStu(id) {
    var stu_name = $("#stu_name").val();
    var Grade = $("#grade").val();
    var Class = $("#Class").val();
    window.location.href = "{:U('public/Index/UpdateStudent')}&id=" + id + "&grade=" + Grade + "&class=" + Class + "&stuName=" + stu_name;
  }
  function DeleteStu(id) {
    if (confirm("你确定要删除这个学生？")) {
      $.ajax({
        type: "POST",
        url: "{:U('public/Index/DeleteStu')}",
        data: "id=" + id,
        dataType: "text",
        success: function (data) {
          if (isNaN(data)) {
            alert(data);
          } else {
            select_filter();
          }
        }
      });
    }
  }
  function updateclass() {
    var class_type = $("#class_type").val();
    var class_id = $("#class_id").val();
    $.ajax({
      type: "POST",
      url: "{:U('public/Index/updateclassType')}",
      data: "class_id=" + class_id + "&class_type=" + class_type,
      dataType: "text",
      success: function (data) {
        if (isNaN(data)) {
          alert(data);
        } else {
          alert("调档成功");
          $("#pop_wrap").css("display", "none");
        }
      }
    });
  }
  function close() {
    $("#pop_wrap").css("display", "none");
  }
</script>
<include file="../Public/_foot" />